package com.app.twth.ui.adjust.remeasure;


import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseCameraPhotoActivity;
import com.app.twth.bean.FilePathListDTO;
import com.app.twth.bean.PhotoBean;
import com.app.twth.databinding.ActivityRemeasureBinding;
import com.app.twth.ui.checkin.PhotoAdapter;
import com.app.twth.ui.tally.sku.SkuUnitDTO;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe :
 */
public class ReMeasureActivity extends BaseCameraPhotoActivity<ActivityRemeasureBinding, ReMeasureViewModel> {
    private String TAG = getClass().getSimpleName();
    private int type = 0;
    private int position = 0;
    private int photoCount = 3;

    private List<PhotoBean> lengthPaths = new ArrayList<>();
    private List<PhotoBean> widthPaths = new ArrayList<>();
    private List<PhotoBean> heightPaths = new ArrayList<>();
    private List<PhotoBean> weightPaths = new ArrayList<>();

    private PhotoAdapter lengthAdapter, widthAdapter, heightAdapter, weightAdapter;

    @Override
    protected void initListener() {
        viewModel.fileUploadMutableLiveData.observe(this, data -> {
            Log.e(TAG, "type():" + data.getType() + ",position:" + data.getPosition());
            switch (data.getType()) {
                case 0:
                    if (lengthPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = lengthPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 1:
                    if (widthPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = widthPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 2:
                    if (heightPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = heightPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 3:
                    if (weightPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = weightPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
            }

            if (data.isSubmit()) {
                submitData();
            }
        });
        viewModel.submitMutableLiveData.observe(this, code -> {
            finish();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint(code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "ReMeasure";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext(getString(R.string.submit));
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        viewModel.reMeasureLiveData.setValue((ReMeasureQr) bundle.getSerializable("data"));
        viewModel.code.setValue(viewModel.reMeasureLiveData.getValue().getBarcode());

        lengthPaths.add(new PhotoBean());
        lengthAdapter = new PhotoAdapter(this, lengthPaths, photoCount, position -> {
            type = 0;
            this.position = position;
            requestCamera("length");
        }, str -> {
            type = 0;
        });
        GridLayoutManager lengthManager = new GridLayoutManager(this, 3);
        binding.rvLength.setLayoutManager(lengthManager);
        binding.rvLength.setAdapter(lengthAdapter);

        widthPaths.add(new PhotoBean());
        widthAdapter = new PhotoAdapter(this, widthPaths, photoCount, position -> {
            type = 1;
            this.position = position;
            requestCamera("width");
        }, str -> {
            type = 1;
        });
        GridLayoutManager widthManager = new GridLayoutManager(this, 3);
        binding.rvWidth.setLayoutManager(widthManager);
        binding.rvWidth.setAdapter(widthAdapter);

        heightPaths.add(new PhotoBean());
        heightAdapter = new PhotoAdapter(this, heightPaths, photoCount, position -> {
            type = 2;
            this.position = position;
            requestCamera("height");
        }, str -> {
            type = 2;
        });
        GridLayoutManager heightManager = new GridLayoutManager(this, 3);
        binding.rvHeight.setLayoutManager(heightManager);
        binding.rvHeight.setAdapter(heightAdapter);

        weightPaths.add(new PhotoBean());
        weightAdapter = new PhotoAdapter(this, weightPaths, photoCount, position -> {
            type = 3;
            this.position = position;
            requestCamera("weight");
        }, str -> {
            type = 3;
        });
        GridLayoutManager weightManager = new GridLayoutManager(this, 3);
        binding.rvWeight.setLayoutManager(weightManager);
        binding.rvWeight.setAdapter(weightAdapter);
        binding.tvLength.setText(viewModel.reMeasureLiveData.getValue().getSkuUnit().getLength() + "");
        binding.tvWidth.setText(viewModel.reMeasureLiveData.getValue().getSkuUnit().getWidth() + "");
        binding.tvHeight.setText(viewModel.reMeasureLiveData.getValue().getSkuUnit().getHeight() + "");
        binding.tvWeight.setText(viewModel.reMeasureLiveData.getValue().getSkuUnit().getWeight() + "");

        //光标后移
        EditTextUtil.setSelection(binding.tvLength);
        EditTextUtil.setSelection(binding.tvWidth);
        EditTextUtil.setSelection(binding.tvHeight);
        EditTextUtil.setSelection(binding.tvWeight);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reset:
                binding.tvLength.setText("");
                binding.tvWidth.setText("");
                binding.tvHeight.setText("");
                binding.tvWeight.setText("");
                binding.tvLength.setTextColor(getResources().getColor(R.color.black));
                binding.tvWidth.setTextColor(getResources().getColor(R.color.black));
                binding.tvHeight.setTextColor(getResources().getColor(R.color.black));
                binding.tvWeight.setTextColor(getResources().getColor(R.color.black));
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                submitData();
                break;
        }
    }


    @Override
    public void getScanninCode() {
        super.getScanninCode();
        setInventory();
    }

    private void setInventory() {
        bottomLayoutBinding.tvHint.setText("");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void getImgPath(String path) {
        setPath(type, path);
    }

    private void setPath(int type, String pathImage) {
        Log.e(TAG, "setPath>type:" + type + ",pathImage:" + pathImage);

        if (!pathImage.contains("http")) {//选择的图片
            String fileName = "";
            switch (type) {
                case 0:
                    fileName = "length";
                    break;
                case 1:
                    fileName = "width";
                    break;
                case 2:
                    fileName = "height";
                    break;
                case 3:
                    fileName = " weight";
                    break;
            }
            Log.e(TAG, "setPath>type:" + type + ",fileName:" + fileName);
            viewModel.fileUpload(viewModel.code.getValue(), fileName, pathImage, type, 0, false, false);
        }

        //lengthAdapter, widthAdapter, heightAdapter, weightAdapter;
        PhotoBean imgUrl = new PhotoBean();
        imgUrl.setPathImg(pathImage);
        imgUrl.setUrlImg(pathImage);
        switch (type) {
            case 0:
                if (lengthPaths.size() > position) {
                    lengthPaths.remove(position);
                    lengthPaths.add(position, imgUrl);
                }
                if (lengthPaths.size() < photoCount) {
                    if (!Utils.isEmpty(lengthPaths.get(lengthPaths.size() - 1).getPathImg())) {
                        lengthPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : lengthPaths) {
                    Log.e(TAG, img.toString());
                }
                if (lengthAdapter != null) {
                    lengthAdapter.setData(lengthPaths);
                }
                break;
            case 1:
                if (widthPaths.size() > position) {
                    widthPaths.remove(position);
                    widthPaths.add(position, imgUrl);
                }
                if (widthPaths.size() < photoCount) {
                    if (!Utils.isEmpty(widthPaths.get(widthPaths.size() - 1).getPathImg())) {
                        widthPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : widthPaths) {
                    Log.e(TAG, img.toString());
                }
                if (widthAdapter != null) {
                    widthAdapter.setData(widthPaths);
                }
                break;
            case 2:
                if (heightPaths.size() > position) {
                    heightPaths.remove(position);
                    heightPaths.add(position, imgUrl);
                }
                if (heightPaths.size() < photoCount) {
                    if (!Utils.isEmpty(heightPaths.get(heightPaths.size() - 1).getPathImg())) {
                        heightPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : heightPaths) {
                    Log.e(TAG, img.toString());
                }
                if (heightAdapter != null) {
                    heightAdapter.setData(heightPaths);
                }
                break;
            case 3:
                if (weightPaths.size() > position) {
                    weightPaths.remove(position);
                    weightPaths.add(position, imgUrl);
                }
                if (weightPaths.size() < photoCount) {
                    if (!Utils.isEmpty(weightPaths.get(weightPaths.size() - 1).getPathImg())) {
                        weightPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : weightPaths) {
                    Log.e(TAG, img.toString());
                }
                if (weightAdapter != null) {
                    weightAdapter.setData(weightPaths);
                }
                break;
        }
    }

    private void submitData() {
        String length = binding.tvLength.getText().toString();
        if (!viewModel.judgeType(length, getResources().getString(R.string.Please_enter_length))) {
            return;
        }
        String width = binding.tvWidth.getText().toString();
        if (!viewModel.judgeType(width, getResources().getString(R.string.Please_enter_width))) {
            return;
        }
        String height = binding.tvHeight.getText().toString();
        if (!viewModel.judgeType(height, getResources().getString(R.string.Please_enter_height))) {
            return;
        }
        String weight = binding.tvWeight.getText().toString();
        if (!viewModel.judgeType(weight, getResources().getString(R.string.Please_enter_weight))) {
            return;
        }

        List<String> lengthPathList = new ArrayList<>();
        if (lengthPaths.size() > 0) {
            for (int i = 0; i < lengthPaths.size(); i++) {
                PhotoBean photoBean = lengthPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.code.getValue(), "length", s, 0, position, true, true);
                        return;
                    }
                    lengthPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.code.getValue(), "length", photoBean.getPathImg(), 0, position, true, true);
                    return;
                }
            }
        }
        if (lengthPathList.size() == 0) {
            ToastUtil.showToast("Please add length photos");
            return;
        }

        List<String> widthPathList = new ArrayList<>();
        if (widthPaths.size() > 0) {
            for (int i = 0; i < widthPaths.size(); i++) {
                PhotoBean photoBean = widthPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.code.getValue(), "width", s, 1, position, true, true);
                        return;
                    }
                    widthPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.code.getValue(), "width", photoBean.getPathImg(), 1, position, true, true);
                    return;
                }
            }
        }
        if (widthPathList.size() == 0) {
            ToastUtil.showToast("Please add  width photos");
            return;
        }

        List<String> heightPathList = new ArrayList<>();
        if (heightPaths.size() > 0) {
            for (int i = 0; i < heightPaths.size(); i++) {
                PhotoBean photoBean = heightPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.code.getValue(), "height", s, 2, position, true, true);
                        return;
                    }
                    heightPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.code.getValue(), "height", photoBean.getPathImg(), 2, position, true, true);
                    return;
                }
            }
        }
        if (heightPathList.size() == 0) {
            ToastUtil.showToast("Please add height photos");
            return;
        }

        List<String> weightPathList = new ArrayList<>();
        if (weightPaths.size() > 0) {
            for (int i = 0; i < weightPaths.size(); i++) {
                PhotoBean photoBean = weightPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(viewModel.code.getValue(), "weight", s, 3, position, true, true);
                        return;
                    }
                    weightPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(viewModel.code.getValue(), "weight", photoBean.getPathImg(), 3, position, true, true);
                    return;
                }
            }
        }
        if (weightPathList.size() == 0) {
            ToastUtil.showToast("Please add weight photos");
            return;
        }

        SkuUnitDTO skuUnit = new SkuUnitDTO();
        skuUnit.setWeight(Double.parseDouble(weight));
        skuUnit.setWidth(Double.parseDouble(width));
        skuUnit.setHeight(Double.parseDouble(height));
        skuUnit.setLength(Double.parseDouble(length));
        viewModel.reMeasureLiveData.getValue().setSkuUnit(skuUnit);

        List<FilePathListDTO> filePathList = new ArrayList<>();

        FilePathListDTO lengthFilePathListDTO = new FilePathListDTO();
        lengthFilePathListDTO.setName("length");
        lengthFilePathListDTO.setPathList(lengthPathList);
        filePathList.add(lengthFilePathListDTO);

        FilePathListDTO widthFilePathListDTO = new FilePathListDTO();
        widthFilePathListDTO.setName("width");
        widthFilePathListDTO.setPathList(widthPathList);
        filePathList.add(widthFilePathListDTO);

        FilePathListDTO heightFilePathListDTO = new FilePathListDTO();
        heightFilePathListDTO.setName("height");
        heightFilePathListDTO.setPathList(heightPathList);
        filePathList.add(heightFilePathListDTO);

        FilePathListDTO weightFilePathListDTO = new FilePathListDTO();
        weightFilePathListDTO.setName("weight");
        weightFilePathListDTO.setPathList(weightPathList);
        filePathList.add(weightFilePathListDTO);
        viewModel.reMeasureLiveData.getValue().setFilePathList(filePathList);

        viewModel.tallyReMeasureSubmit();
    }

}
